﻿using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using Me.Amon.C;
using Me.Amon.POS;

namespace Me.Amon.Util
{
    public class WebUtil
    {
        private static Properties.Settings _Settings;

        public static string POST(string url, string data)
        {
            try
            {
                WebClient webClient = new WebClient();
                webClient.Encoding = Encoding.UTF8;

                if (string.IsNullOrWhiteSpace(data))
                {
                    return webClient.DownloadString(url);
                }

                //byte[] post = Encoding.UTF8.GetBytes(data);//编码，尤其是汉字，事先要看下抓取网页的编码方式  
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header，如果改为GET方式的话就去掉这句话即可  
                return webClient.UploadString(url, data);
                //byte[] resp = webClient.UploadData(url, "POST", post);//得到返回字符流  
                //return Encoding.UTF8.GetString(resp);//解码 
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string Select(string url, string data)
        {
            if (_Settings == null)
            {
                _Settings = Properties.Settings.Default;
            }

            return POST(_Settings.Server + url + string.Format("?{0}={1}&{2}={3}", PPos.PARAM_KEY_CORP, _Settings.CorpCode, CObj.PARAM_KEY_OPT, PPos.PARAM_VALUE_OPT_SELECT),
                PPos.PARAM_KEY_DATA + '=' + data);
        }

        public static string Update(string url, string data)
        {
            if (_Settings == null)
            {
                _Settings = Properties.Settings.Default;
            }

            return POST(_Settings.Server + url + string.Format("?{0}={1}&{2}={3}", PPos.PARAM_KEY_CORP, _Settings.CorpCode, CObj.PARAM_KEY_OPT, PPos.PARAM_VALUE_OPT_UPDATE),
                PPos.PARAM_KEY_DATA + '=' + data);
        }

        public static string GetIp()
        {
            string host = Dns.GetHostName();

            //得到本机IP地址数组
            var ipEntry = Dns.GetHostAddresses(host);
            if (ipEntry != null)
            {
                foreach (var ip in ipEntry)
                {
                    host = ip.ToString();
                    if (IsIPv4(host))
                    {
                        return host;
                    }
                }
            }
            return "";
        }

        public static bool IsIPv4(string addr)
        {
            //利用正则表达式判断字符串是否符合IPv4格式
            if (!Regex.IsMatch(addr, "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$"))
            {
                return false;
            }

            //根据小数点分拆字符串
            foreach (var ip in addr.Split('.'))
            {
                if (int.Parse(ip) > 255)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
